import {messages} from "share/common";
import React, {Component} from 'react';
import {Form, Button, Checkbox, Spin} from 'antd'
import {connect} from 'react-redux'
import NewConsumerServer from 'containers/supplier-management/new-consumer-management/new-consumer-server'

const FormItem = Form.Item;
const Group = Checkbox.Group;

@Form.create()
class CtripSetting extends Component {

  constructor(props) {
    super(props);
    this.saveOption = {
      tenantId: props.tenant.id,
    }
    this.state = {
      loading: false,
      data: {},// 设置数据
    }
  }

  componentWillMount() {
    this.initSetData();
  }

  // 初始化或者刷新设置数据
  initSetData = () => {
    NewConsumerServer(this).getPreControl(this.props.tenant.id).then(res => {
      this.setState({data: res.data.body}, this.props.form.resetFields);
    })
  }

  // 保存设置数据
  onSaveData = () => {
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        const params = Object.assign({}, values, this.saveOption);
        NewConsumerServer(this).saveSetPreControl(params).then(res => {
          this.initSetData();
        })
      }
    })
  }

  render() {
    const {getFieldDecorator} = this.props.form;
    const {loading, data} = this.state;
    const formLayout = {
      wrapperCol: {span: 18},
      labelCol: {span: 2},
      style: {marginBottom: 0},
    }
    return (
      <div>
        <Spin spinning={loading}>
          <div style={{border: '1px solid #e8e8e8', borderRadius: 2, padding: '16px 24px'}}>
            <h3 style={{marginLeft: 6}}>{messages('supplier.key621')/*审批单前置管控*/}</h3>
            <Form>
              <FormItem label={messages('supplier.key443')/*机票*/} {...formLayout}>
                {
                  getFieldDecorator('airPreVerifyFields', {
                    initialValue: data.airPreVerifyFields || [],
                  })(
                    <Group>
                      <Checkbox style={{marginTop: 6}} value='DATE'>{messages('common.date')/*日期*/}</Checkbox>
                      <Checkbox value='PASSENGERLIST'>{messages('supplier.key622')/*出行人*/}</Checkbox>
                      <Checkbox value='FLIGHT'>{messages('supplier.key623')/*出发、到达城市*/}</Checkbox>
                      <Checkbox value='SEATCLASS'>{messages('supplier.key624')/*舱等*/}</Checkbox>
                    </Group>
                  )
                }
              </FormItem>
              <FormItem label={messages('supplier.key444')/*酒店*/} {...formLayout}>
                {
                  getFieldDecorator('hotelPreVerifyFields', {
                    initialValue: data.hotelPreVerifyFields || [],
                  })(
                    <Group>
                      <Checkbox style={{marginTop: 6}} value='DATE'>{messages('common.date')/*日期*/}</Checkbox>
                      <Checkbox value='PASSENGERLIST'>{messages('supplier.key625')/*入住人*/}</Checkbox>
                      <Checkbox value='CHECKINCITYCODES'>{messages('supplier.key626')/*入住城市*/}</Checkbox>
                    </Group>
                  )
                }
              </FormItem>
            </Form>
          </div>
          <Button style={{margin: 8}} type='primary' onClick={this.onSaveData}>{messages('supplier.key627')/*保存设置*/}</Button>
        </Spin>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  }
}

export default connect(mapStateToProps)(CtripSetting);
